package com.cola.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class DBControl
{
	private static Logger logger = LoggerFactory.getLogger(DBControl.class);

	//main数据源
	private static DataSource dataSource;

	public DBControl(){

	}

    static{
        //初始化数据库配置
        initMain();
    }

    //初始化主库连接
    private static void initMain(){
        try{
            InputStream in = DBControl.class.getClassLoader()
                    .getResourceAsStream("db.properties");
            Properties props = new Properties();
            props.load(in);
            dataSource = DruidDataSourceFactory.createDataSource(props);
        }catch(Exception ex){
            logger.error("",ex);
        }
    }

    /**
     * 获取main 库连接
     * @return
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }


    public static void main(String[]args) throws SQLException {
    }
}
